package com.usian.article.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.usian.apis.article.AuthorControllerApi;
import com.usian.article.service.ApAuthorService;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/api/v1/author")
public class AuthorController implements AuthorControllerApi {

    @Autowired
    private ApAuthorService apAuthorService;


    /**
     * 根据社交账号ID 查询作者信息
     *
     * @param id
     * @return
     */
    @Override
    @GetMapping("/findByUserId")
    public ApAuthor findByUserId(@RequestParam Integer id) {
        return apAuthorService.findByUserId(id);
    }

    /**
     * 作者保存
     *
     * @param apAuthor
     * @return
     */
    @Override
    @PostMapping("/save")
    public ResponseResult saveApauthor( @RequestBody ApAuthor apAuthor) {
        apAuthor.setCreatedTime(new Date());
        boolean result = apAuthorService.save(apAuthor);
        if(result){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }

    /**
     * 根据用户名称查询作者信息
     *
     * @param name
     * @return
     */
    @GetMapping("/findByName/{name}")
    @Override
    public ApAuthor findByName(@PathVariable("name") String name) {
        QueryWrapper<ApAuthor> queryWrapper =new QueryWrapper<>();
        queryWrapper.lambda().eq(ApAuthor::getName,name);

        ApAuthor apAuthor = apAuthorService.getOne(queryWrapper);
        return apAuthor;
    }

    /**
     * 根据用户id 查询作者信息
     *
     * @param id
     * @return
     */
    @GetMapping("/one/{id}")
    @Override
    public ApAuthor findById(@PathVariable("id") Integer id) {
        ApAuthor apAuthor = apAuthorService.getById(id);
        return apAuthor;
    }

}
